﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using WinFormsUI.Docking;
using ChaSQLite.SubForm;
using InterMessage;
using ChaSQLite.Constants;

namespace ChaSQLite
{
    public partial class MainForm : Form, IMessageClient
    {
        private IntroForm introViewer = new IntroForm();
        private TableListViewer tableListViewer = new TableListViewer();
        private HistoryViewer historyViewer = null;
        private TableWizardViewer tableWizardViewer = null;

        private DeserializeDockContent deserializeDockContent;

        private Hashtable ddlHash = new Hashtable();

        public MainForm()
        {
            InitializeComponent();
            deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
        }

        public void ReceiveMessage(int type, Object message)
        {
            MSG_CONSTANT nType = (MSG_CONSTANT)type;
            switch (nType)
            { 
                case MSG_CONSTANT.MSG_DB_OPEN:
                    menuItemQuery.Enabled = true;
                    menuItemOpenDB.Enabled = false;
                    menuItemCloseDB.Enabled = true;
                    break;
                case MSG_CONSTANT.MSG_DB_CLOSE:
                    menuItemQuery.Enabled = false;
                    menuItemOpenDB.Enabled = true;
                    menuItemCloseDB.Enabled = false;
                    break;
                case MSG_CONSTANT.MSG_TABLE_DDL:
                    showDDL( (string)message );
                    break;
            }
        }
        
        private void MainForm_Load(object sender, EventArgs e)
        {
            BroadCaster.GetInstance.Attach(this);

            loadForm();
        }

        private IDockContent GetContentFromPersistString(string persistString)
        {
            /*
            if (persistString == typeof(DummySolutionExplorer).ToString())
                return m_solutionExplorer;
            else if (persistString == typeof(DummyPropertyWindow).ToString())
                return m_propertyWindow;
            else if (persistString == typeof(DummyToolbox).ToString())
                return m_toolbox;
            else if (persistString == typeof(DummyOutputWindow).ToString())
                return m_outputWindow;
            else if (persistString == typeof(DummyTaskList).ToString())
                return m_taskList;
            else*/
            {
                // DummyDoc overrides GetPersistString to add extra information into persistString.
                // Any DockContent may override this value to add any needed information for deserialization.

                string[] parsedStrings = persistString.Split(new char[] { ',' });
                if (parsedStrings.Length != 3)
                    return null;

                if (parsedStrings[0] != typeof(IntroForm).ToString())
                    return null;

                
                IntroForm dummyDoc = new IntroForm();
                /*
                if (parsedStrings[1] != string.Empty)
                    dummyDoc.FileName = parsedStrings[1];
                if (parsedStrings[2] != string.Empty)
                    dummyDoc.Text = parsedStrings[2];
                */
                return dummyDoc;
            }
        }

        private void loadForm()
        {
            tableListViewer.Show(dockPanel);
            introViewer.Show(dockPanel);
        }

        private void menuItemOpenDB_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            fd.InitialDirectory = "c:\\";
            fd.Filter = "SQLite Database Files |*.db3|All files (*.*)|*.*";
            fd.FilterIndex = 1;
            fd.RestoreDirectory = true;

            if (fd.ShowDialog() == DialogResult.Cancel)
                return;

            BroadCaster.GetInstance.SendMessage(this, (int)MSG_CONSTANT.MSG_DB_OPEN, fd.FileName );
        }

        private void menuItemQuery_Click(object sender, EventArgs e)
        {
            DockContent dummyDoc = CreateNewDocument();            

            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                dummyDoc.MdiParent = this;
                dummyDoc.Show();
            }
            else
                dummyDoc.Show(dockPanel);
        }

        private DockContent CreateNewDocument()
        {
            //QueryViewer viewer = new QueryViewer();
            DynamicQueryViewer viewer = new DynamicQueryViewer();

            Application.AddMessageFilter(viewer);

            int count = 1;
            //string text = "C:\\MADFDKAJ\\ADAKFJASD\\ADFKDSAKFJASD\\ASDFKASDFJASDF\\ASDFIJADSFJ\\ASDFKDFDA" + count.ToString();
            string text = "QueryEditor@" + count.ToString();
            viewer.Title = text;
            while (FindDocument(text) != null)
            {
                count++;
                //text = "C:\\MADFDKAJ\\ADAKFJASD\\ADFKDSAKFJASD\\ASDFKASDFJASDF\\ASDFIJADSFJ\\ASDFKDFDA" + count.ToString();
                text = "QueryEditor@" + count.ToString();
                viewer.Title = text;
            }
            viewer.Text = text;
            return viewer;
        }

        private IDockContent FindDocument(string text)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                    if (form.Text == text)
                        return form as IDockContent;

                return null;
            }
            else
            {
                foreach (IDockContent content in dockPanel.Documents)
                    if (content.DockHandler.TabText == text)
                        return content;

                return null;
            }
        }

        private void menuItemCloseDB_Click(object sender, EventArgs e)
        {
            BroadCaster.GetInstance.SendMessage(this, (int)MSG_CONSTANT.MSG_DB_CLOSE, null);
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }

        private void menuFileExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemUpgradeInfo_Click(object sender, EventArgs e)
        {
            UpgradeViewer viewer = new UpgradeViewer();
            viewer.ShowDialog();
        }

        private void menuItemHistory_Click(object sender, EventArgs e)
        {
            if (historyViewer == null)
            {
                historyViewer = new HistoryViewer();
                historyViewer.Show(dockPanel);
            }

            menuItemHistory.Checked = menuItemHistory.Checked == true ? false : true;
            if (menuItemHistory.Checked)
                historyViewer.Show();
            else
                historyViewer.Hide();
        }

        private void showDDL(string tableName)
        {
            if (ddlHash.ContainsKey(tableName) == true)
                return;

            ddlHash.Add(tableName, tableName);
            
            DDLViewer ddlViewer = new DDLViewer();
            ddlViewer.Title = tableName;
            ddlViewer.Show(dockPanel);
        }

        private void menuItemCreateTable_Click(object sender, EventArgs e)
        {
            tableWizardViewer = new TableWizardViewer();
            tableWizardViewer.Show(dockPanel);
        }
    }
}

